package fabrica2.web.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fabrica2.modelo.Cliente;
import fabrica2.modelo.Usuario;
import fabrica2.web.rmi.cliente.ServicosWebCliente;
import fabrica2.web.rmi.execoes.WebRMIException;

/**
 * Servlet implementation class CadastrarClienteController
 */
public class CadastrarClienteController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	

	/**
	 * Default constructor.
	 */
	public CadastrarClienteController() {
		// TODO Auto-generated constructor stub
	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String nome = request.getParameter("nome");
		String tipoDocumento = request.getParameter("tipoDocumento");
		String codDocumento = request.getParameter("codDocumento");
		String endereco = request.getParameter("endereco");
		String telefone = request.getParameter("telefone");
		String email = request.getParameter("email");
		
		Cliente cliente = new Cliente(0, tipoDocumento, codDocumento, nome, endereco, telefone, email);
		 
		try {
			ServicosWebCliente servicos = new ServicosWebCliente();
			servicos.cadastrarCliente(cliente);
			request.setAttribute("msg", "Cadastro realizado com sucesso");
			RequestDispatcher rd = request.getRequestDispatcher("/cadastro.jsp");
			rd.forward(request, response);
		} catch (WebRMIException e) {
			request.setAttribute("msg", "Erro no cadastro");
			RequestDispatcher rd = request.getRequestDispatcher("/cadastro.jsp");
			rd.forward(request, response);
		}
		
	}

}
